Applescript Variable Error

Hi all,


I am trying to write an applescript that is called with a variable and then uses that variable in the script. For example:


on run argv 
  set a to item 1 of argv 
  tell application "iTerm-2" 
    tell current window 
      tell the current session 
      write text "ssh" & a 
      end tell 
    end tell 
  end tell 
end run


So the app would be called on the command line: open mypp.app --args machine. The app iTerm-2 would open and run the command "ssh machine". At the moment it launches iTerm-2 and runs the ssh command but ignores the variable. Am I called the variable of "a" incorrectly?


Thanks,
Sam

Posted on Jul 21, 2016 3:14 AM

Reply
4 replies

Jul 21, 2016 6:47 AM in response to VikingOSX

OK - so that worked! However I am a bit up the creek now. I have a software solution that when I am logged into an define a "launcher" which fires up a Terminal Emulator on my Mac. I am pointing this software to my script which as you can see fires up the emulator and gives it a command to run. This software also hands over an ID and password as an argument which can be added to the command to run. Unfortunately it looks like it uses "open -a" and I can't change that. Should open -args be working or is this a problem to be fixed in the future?

Jul 21, 2016 9:24 AM in response to srudland

This also works to get machine into your script. It will still work without the on run argv block. Use quotes for arguments containing white-space. You can adapt this to parse multiple arguments of different data types from a single command line.


-- reference: http://unixjunkie.blogspot.com/2006/07/command-line-processing-in-cocoa.html

-- reference: http://perspx.com/archives/parsing-command-line-arguments-nsuserdefaults/

-- Terminal invocation: open mypp.app --args -stringArg machine


use scripting additions

use framework "Foundation"


on run argv

set argx to current application'sNSUserDefaults'sstandardUserDefaults()

set sarg to (argx's stringForKey:"stringArg") as text

if sarg is not "" then

display dialogsarg

end if

end run

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript Variable Error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.